package com.yxwl.demo.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.widget.NestedScrollView;

import com.print.base.bean.TaskCallBean;
import com.print.base.listen.TaskCallback;
import com.yxwl.demo.R;
import com.yxwl.demo.ui.print.printer.PrinterUtil;
import com.yxwl.demo.utils.StringUtil;

import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class LogView extends FrameLayout {

    @SuppressLint("StaticFieldLeak")
    public static WeakReference<LogView> logViewWR;

    private SimpleDateFormat simpleDateFormat;
    private TextView tv;
    private NestedScrollView sv;

    public LogView(Context context) {
        this(context, null);
    }

    public LogView(Context context, @Nullable AttributeSet attrs) {
        this(context, null, 0);
    }

    public LogView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void init() {
        tv = findViewById(R.id.console);
        sv = findViewById(R.id.scrollview);
        findViewById(R.id.clear).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                tv.setText("");
            }
        });

        simpleDateFormat = new SimpleDateFormat("mm:ss SSS", Locale.CHINA);
        PrinterUtil.getInstance().addTaskCall(taskCallback);
        if (logViewWR != null && logViewWR.get() != null) {
            PrinterUtil.getInstance().removeTaskCall(logViewWR.get().taskCallback);
        }
        logViewWR = new WeakReference<>(this);
    }

    TaskCallback taskCallback = new TaskCallback() {
        @Override
        public void sendStatus(TaskCallBean bean) {
            log("sendStatus MSG : " + bean.msg);
        }

        @Override
        public void readCall(TaskCallBean bean) {
            log("全局 读取>> type: " + bean.type + ", msg: " + bean.msg
                    + ", str: " + new String(bean.data)
                    + ", hex: " + StringUtil.bytetToHex(bean.data));
        }
    };

    public static void log(String txt) {
        if (logViewWR != null && logViewWR.get() != null) {
            logViewWR.get().mylog(txt);
        }
    }

    public void mylog(String txt) {
        if (StringUtil.isSpace(txt)) {
            return;
        }
        try {
            Activity act = (Activity) getContext();
            if (act == null || act.isFinishing()) {
                return;
            }
            act.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    String msg = simpleDateFormat.format(new Date()) + ": " + txt;
                    if (txt.contains("发送")) {
                        Log.d("测试 logMsg", txt);
                    } else {
                        Log.e("测试 logMsg", txt);
                    }
                    try {
                        String s = tv.getText().toString().trim();
                        if (TextUtils.isEmpty(s)) {
                            tv.setText(msg);
                        } else if (s.length() > 5000) {
                            s = "日志定时清理完成...\n\n" + msg;
                            tv.setText(s);
                        } else {
                            tv.append("\n\n" + msg);
                        }
                        sv.post(new Runnable() {
                            public void run() {
                                sv.fullScroll(130);
                            }
                        });
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception ignored) {
        }
    }
}
